/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.blog.city.checkthedep.managedbeans;

import com.blog.city.checkthedep.entities.RegisteredUser;
import com.blog.city.checkthedep.sessionbeans.LoginSessLocal;
import com.blog.city.checkthedep.sessionbeans.RegisteredUserFacadeLocal;
import javax.ejb.EJB;
import javax.faces.bean.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import org.primefaces.context.RequestContext;

/**
 *
 * @author Breath
 */
@ManagedBean
@SessionScoped
public class LoginBean {

    private RegisteredUser checkedUser = new RegisteredUser();
    private String checkedUsername;
    private String checkedPassword;
    private boolean status = false;
    private String menu="general_Menu";
   


    /**
     * 
     * @return
     */
    public String getMenu() {
        return menu;
    }

    /**
     * 
     * @param menu
     */
    public void setMenu(String menu) {
        this.menu = menu;
    }

    /**
     * 
     * @return
     */
    public boolean isStatus() {
        return status;
    }

    /**
     * 
     * @param status
     */
    public void setStatus(boolean status) {
        this.status = status;
    }
    @EJB
    private RegisteredUserFacadeLocal regUserFa;
    @EJB
    private LoginSessLocal logSession;

    /**
     * 
     * @return
     */
    public String getCheckedUsername() {
        return checkedUsername;
    }

    /**
     * 
     * @param checkedUsername
     */
    public void setCheckedUsername(String checkedUsername) {
        this.checkedUsername = checkedUsername;
    }

    /**
     * 
     * @return
     */
    public String getCheckedPassword() {
        return checkedPassword;
    }

    /**
     * 
     * @param checkedPassword
     */
    public void setCheckedPassword(String checkedPassword) {
        this.checkedPassword = checkedPassword;
    }

    /**
     * 
     */
    public void retrieveUser() {
        this.setCheckedUser(regUserFa.findRegUserByUsername(checkedUsername));
    }

    /**
     * 
     * @return
     */
    public RegisteredUser getCheckedUser() {
        return checkedUser;
    }

    /**
     * 
     * @param checkedUser
     */
    public void setCheckedUser(RegisteredUser checkedUser) {
        this.checkedUser = checkedUser;
    }

    /**
     * 
     * @return
     */
    public RegisteredUserFacadeLocal getRegUserFa() {
        return regUserFa;
    }

    /**
     * 
     * @param regUserFa
     */
    public void setRegUserFa(RegisteredUserFacadeLocal regUserFa) {
        this.regUserFa = regUserFa;
    }

    /**
     * 
     */
    public void loginUser() {
        RequestContext context = RequestContext.getCurrentInstance();
        FacesMessage msg = null;
        retrieveUser();
        if (status == true) {
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "You are already logged in ", checkedUser.getUsername());
        } else if (loggedIn() == true) {
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Welcome", checkedUser.getUsername());
            changeMenu();
            context.addPartialUpdateTarget("regMenu");
            context.addPartialUpdateTarget("editProf");
            
        } else {
            msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Login Error", "Invalid credentials");
        }
        FacesContext.getCurrentInstance().addMessage(null, msg);
        context.addCallbackParam("loggedIn", loggedIn());   
    }

    /**
     * 
     * @return
     */
    public boolean loggedIn() {
        return logSession.loginUserSession(this.getCheckedUser(), this.getCheckedPassword(), status);
    }

    /**
     * 
     * @return
     */
    public boolean logout() {
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("loginBean");
        return true;
    }

    /**
     * 
     */
    public void changeMenu() {
            this.setMenu("registeredMember_Menu"); 
    }
}
